<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['caiwu:accounts:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['caiwu:accounts:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['caiwu:accounts:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['caiwu:accounts:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-switch
          v-model="isCardView"
          inline-prompt
          :active-icon="Grid"
          :inactive-icon="List"
          @change="handleViewChange"
        />
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格视图 -->
    <el-table 
      v-if="!isCardView"
      v-loading="loading" 
      :data="accountsList" 
      @selection-change="handleSelectionChange"
      border
      :cell-style="{ padding: '8px' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账套id" align="center" prop="accountId" />
      <el-table-column label="账套名称" align="center" prop="accountName" />
      
      <el-table-column label="账套启用日期" align="center" prop="startDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属行业" align="center" prop="industry" show-overflow-tooltip>
        <template #default="scope">
          <div class="cell-content">{{ scope.row.industry }}</div>
        </template>
      </el-table-column>
      <el-table-column label="账套启用日期" align="center" prop="startDate" width="120">
        <template #default="scope">
          <div class="cell-content">{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="会计制度" align="center" prop="accountingSystem" width="120">
        <template #default="scope">
          <div class="cell-content">
            <dict-tag :options="sys_account" :value="scope.row.accountingSystem"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="币别" align="center" prop="currencyName" width="100">
        <template #default="scope">
          <div class="cell-content">{{ scope.row.currencyName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['caiwu:accounts:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['caiwu:accounts:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 卡片视图 -->
    <el-row v-else :gutter="16" class="card-view">
      <el-col v-for="item in accountsList" :key="item.accountId" :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
        <el-card class="account-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span class="account-name">{{ item.accountName }}</span>
              <div class="card-actions">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(item)" v-hasPermi="['caiwu:accounts:edit']" />
                <el-button link type="primary" icon="Delete" @click="handleDelete(item)" v-hasPermi="['caiwu:accounts:remove']" />
              </div>
            </div>
          </template>
          <div class="card-content">
            <div class="info-item">
              <span class="label">所属行业:</span>
              <span class="value">{{ item.industry }}</span>
            </div>
            <div class="info-item">
              <span class="label">启用日期:</span>
              <span class="value">{{ parseTime(item.startDate, '{y}-{m}-{d}') }}</span>
            </div>
            <div class="info-item">
              <span class="label">会计制度:</span>
              <span class="value">
                <dict-tag :options="sys_account" :value="item.accountingSystem"/>
              </span>
            </div>
            <div class="info-item">
              <span class="label">币别:</span>
              <span class="value">{{ item.currencyName }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改账套对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="accountsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账套名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入账套名称" />
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入所属行业" />
        </el-form-item>
        <el-form-item label="采用的会计制度" prop="accountingSystem">
          <el-select v-model="form.accountingSystem" placeholder="请选择采用的会计制度">
            <el-option
              v-for="dict in sys_account"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="币别_id" prop="currencyId">
          <el-input v-model="form.currencyId" placeholder="请输入币别_id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Accounts">
import { listAccounts, getAccounts, delAccounts, addAccounts, updateAccounts } from "@/api/caiwu/accounts";
import { Grid, List } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance();
const { sys_account } = proxy.useDict('sys_account');

const accountsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const isCardView = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    accountName: [
      { required: true, message: "账套名称不能为空", trigger: "blur" }
    ],
    startDate: [
      { required: true, message: "账套启用日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询账套列表 */
function getList() {
  loading.value = true;
  listAccounts(queryParams.value).then(response => {
    accountsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    accountId: null,
    accountName: null,
    industry: null,
    startDate: null,
    accountingSystem: null,
    currencyId: null,
    userId: null
  };
  proxy.resetForm("accountsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.accountId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加账套";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _accountId = row.accountId || ids.value
  getAccounts(_accountId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改账套";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["accountsRef"].validate(valid => {
    if (valid) {
      if (form.value.accountId != null) {
        updateAccounts(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAccounts(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _accountIds = row.accountId || ids.value;
  proxy.$modal.confirm('是否确认删除账套编号为"' + _accountIds + '"的数据项？').then(function() {
    return delAccounts(_accountIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('caiwu/accounts/export', {
    ...queryParams.value
  }, `accounts_${new Date().getTime()}.xlsx`)
}

// 视图切换处理函数
const handleViewChange = (value) => {
  isCardView.value = value;
};

getList();
</script>

<style scoped>
.card-view {
  margin-top: 16px;
}

.account-card {
  margin-bottom: 16px;
  transition: all 0.3s;
}

.account-card:hover {
  transform: translateY(-4px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.account-name {
  font-weight: bold;
  font-size: 16px;
}

.card-actions {
  display: flex;
  gap: 8px;
}

.card-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-item .label {
  color: var(--el-text-color-secondary);
  font-size: 14px;
  min-width: 70px;
}

.info-item .value {
  color: var(--el-text-color-primary);
  font-size: 14px;
  flex: 1;
  background-color: var(--el-fill-color-lighter);
  padding: 4px 8px;
  border-radius: 4px;
}
</style>
